/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.twitter.client;

import org.json.JSONObject;
import org.webos.java.base.Dialog;
import org.webos.java.base.DialogType;
import org.webos.java.base.RemoteMessage;
import org.webos.java.root.Log;
import org.webos.java.base.NetworkManager;
import org.webos.java.base.SystemManager;
import org.webos.java.twitter.Twitter;

/**
 *
 * @author Kostya
 */
@DialogType(jsClass="twitter.SettingsDialog")
public class SettingsDialog extends Dialog<TwitterApp>{

    private String settingName = null;
    private JSONObject data = new JSONObject();
    private Twitter twitter = new Twitter();

    public SettingsDialog(TwitterApp app, String aSettingName) {
        super(app);
        SystemManager.setupProxy(app.getSession());
        this.settingName = aSettingName;
        try {
            data = new JSONObject(app.getOption(settingName, "{}"));
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        try {
            data.put("minutes", data.optInt("minutes", 15));
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        if(twitter.getRequestToken()){
            data.putOpt("url", twitter.getAuthorizationURL());
        }else{
            getApplication().sendError(710, "Oops, something went wrong, can't connect to Twitter OAuth service");
            if(getApplication().getOption(settingName, null)==null)
                getApplication().stopApplication(null);
            return;
        }
        data.putOpt("show_notify", data.optBoolean("show_notify", true));
        setData(data);
        show();
    }

    @RemoteMessage
    public JSONObject verifyClick(JSONObject aData){
        try {
            if(twitter.getAccessToken()){
                Twitter.User user = twitter.getCurrentUser();
                JSONObject object = new JSONObject();
                object.putOpt("name", user.getScreenName());
                return object;
            }
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return NetworkManager.getErrorObject(711, "Can't verify authorization");
    }

    @RemoteMessage
    public JSONObject okClick(JSONObject aData){
        try {
            if(twitter.getToken()==null){
                twitter.setToken(data.optString("token", null), data.optString("tokenSecret", null));
            }
            String userName = twitter.getCurrentUser().getScreenName();
            int minutes = aData.optInt("minutes", 5);
            if(minutes<5)
                minutes = 5;
            Log.debug(this, "Ready to save settings:", userName, minutes);
            data.put("minutes", minutes);
            data.put("username", userName);
            if(twitter.getToken()!=null){
                data.put("token", twitter.getToken());
                data.put("tokenSecret", twitter.getTokenSecret());
            }
            data.put("show_notify", aData.optBoolean("show_notify", true));
            data.put("url", "");
            getApplication().setOption(settingName, data.toString());
            closeDialog(data);
            getApplication().loadApplication();
            return null;
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return NetworkManager.getErrorObject(712, "Can't save twitter settings");
    }

    @RemoteMessage
    public void cancelClick(JSONObject aData){
        if(getApplication().getOption(settingName, null)==null){
            getApplication().getSession().removeDialog(this);
            getApplication().stopApplication(null);
        }else{
            getApplication().getSession().removeDialog(this);
        }
    }

}
